package spotGUI.mainFrameGUI;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import spotGUI.utils.SocialUserListCellRenderer;
import spotGUI.utils.SocialUserSelectionListener;
import sun.awt.RepaintArea;
import utils.MaxLenghtTextDocument;
import utils.SocialUserData;
import java.util.ResourceBundle;

public class SocialNetworkSideBar extends JPanel {

	private JList list;
	private Box boxSearch;
	private JLabel lblSearch;
	private Component horizontalStrut;
	private JTextField textFieldSearch;
	private JLabel lblSearchIcon;

	private JScrollPane scrollPane;
	private DefaultListModel defaultModel;

	public SocialNetworkSideBar() {
		setBorder(new EmptyBorder(10, 10, 10, 10));



		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{199, 0};
		gridBagLayout.rowHeights = new int[]{38, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		boxSearch = Box.createHorizontalBox();
		GridBagConstraints gbc_boxSearch = new GridBagConstraints();
		gbc_boxSearch.fill = GridBagConstraints.HORIZONTAL;
		gbc_boxSearch.insets = new Insets(0, 0, 5, 0);
		gbc_boxSearch.gridx = 0;
		gbc_boxSearch.gridy = 0;
		add(boxSearch, gbc_boxSearch);

		lblSearchIcon = new JLabel("");
		lblSearchIcon.setIcon(new ImageIcon(SocialNetworkSideBar.class.getResource("/images/Icons/32x32px/search.png")));
		boxSearch.add(lblSearchIcon);

		lblSearch = new JLabel(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("SocialNetworkSideBar.lblSearch.text")); //$NON-NLS-1$ //$NON-NLS-2$
		boxSearch.add(lblSearch);

		horizontalStrut = Box.createHorizontalStrut(20);
		boxSearch.add(horizontalStrut);

		textFieldSearch = new JTextField();
		textFieldSearch.setDocument(new MaxLenghtTextDocument(30));
		textFieldSearch.setPreferredSize(new Dimension(100, 20));
		textFieldSearch.setMinimumSize(new Dimension(20, 20));
		textFieldSearch.setMaximumSize(new Dimension(150, 20));
		textFieldSearch.setColumns(10);
		textFieldSearch.setBackground(UIManager.getColor("Button.background"));
		textFieldSearch.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				showOnlyMachingCells(textFieldSearch.getText());
				revalidate();
				repaint();
			}
		});

		boxSearch.add(textFieldSearch);

		scrollPane = new JScrollPane();
		scrollPane.getViewport().setOpaque(false);
		scrollPane.setOpaque(false);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		add(scrollPane, gbc_scrollPane);

		list = new JList();
		list.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		list.setOpaque(false);
		scrollPane.setViewportView(list);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.addListSelectionListener(new SocialUserSelectionListener());


		list.setCellRenderer(new SocialUserListCellRenderer());
		list.setModel(new DefaultListModel());


	}

	private void showOnlyMachingCells(String key) {

		restoreList();

		if (key.length() > 2) {
			DefaultListModel m = new DefaultListModel();
			String lowerCaseKey = key.toLowerCase();
			for (int i = 0; i < ((DefaultListModel)list.getModel()).size(); i++) {
				SocialUserData temp = ((SocialUserData)((DefaultListModel)list.getModel()).get(i));
				if (temp.getFirstName().toLowerCase().startsWith(lowerCaseKey) || temp.getSurname().toLowerCase().startsWith(lowerCaseKey) ||
						(temp.getFirstName().toLowerCase()+" "+temp.getSurname()).toLowerCase().startsWith(lowerCaseKey)) {
					m.addElement(((DefaultListModel)list.getModel()).get(i));
				}
			}
			list.setModel(m);
		}
	}



	public void fillSidebar(HashMap<Integer, SocialUserData> users) {
		defaultModel = new DefaultListModel();

		Collection<SocialUserData> values = users.values();
		Iterator<SocialUserData> it = values.iterator();
		while (it.hasNext()) {
			defaultModel.addElement((it.next()));
		}
		restoreList();
	}

	private void restoreList() {
		list.setModel(defaultModel);
	}


}
